#include "../LeetCodeShell/common.h"

#include <unordered_set>;
using std::unordered_set;

int longestConsecutive(vector<int> &num)
{
	unordered_set<int> remain;
	for(int i=0; i<num.size(); i++){
		remain.insert(num[i]);
	}

	int left;
	int right;
	int maxLen = 0;
	while(remain.size()>0){
		unordered_set<int>::iterator it = remain.begin();
		left = *it-1;
		right = *it+1;
		remain.erase(it);
		while((it=remain.find(left))!=remain.end()){
			left--;
			remain.erase(it);
		}
		while((it=remain.find(right))!=remain.end()){
			right++;
			remain.erase(it);
		}
		int len = right-left-1;
		maxLen = maxLen<len? len : maxLen;
	}

	return maxLen;
}
